<?php
namespace app\admin\controller;
use think\Db;
class Customer extends Base{
    public function customer_list(){
        $key=input('get.key');
        if (!empty(input('get.fromtime'))) {
            $fromtime=strtotime(input('get.fromtime'));
        }else{
            $fromtime=0;
        }
        if (!empty(input('get.totime'))) {
            $totime=strtotime(input('get.totime'))+86400;
        }else{
            $totime=time();
        }
	        $list=Db::name('customer')->where('time','>',"{$fromtime}")->where('time','<',"{$totime}")->where('cname|cphone|cidcard','like',"%{$key}%")->order('id desc')->paginate(10);
	        $lists=Db::name('customer')->where('time','>',"{$fromtime}")->where('time','<',"{$totime}")->where('cname|cphone|cidcard','like',"%{$key}%")->select();
        $this->assign('key',input('get.key'));
        $this->assign('fromtime',input('get.fromtime'));
        $this->assign('totime',input('get.totime'));
        $this->assign('list',$list);
        $this->assign('lists',$lists);
        return $this->fetch();
    }
    public function customer_add(){
        if(request()->isGet()){
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['cname']=$param['cname'];
            $data['cphone']=$param['cphone'];
            $data['cidcard']=$param['cidcard'];
            $data['password']=md5('tmd'.$param['password']);
            $data['status']=1;
            $data['time']=time();
            $res=Db::name('customer')->insert($data);
            return json(['code' => 200, 'data' =>$res, 'msg' => '调试']);
        }
    }
    public function customer_edit(){
        if(request()->isGet()){
            $param = input('get.');
            $user=Db::name('customer')->where('id',$param['id'])->find();
            $this->assign('user',$user);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['cname']=$param['cname'];
            $data['cphone']=$param['cphone'];
            $data['cidcard']=$param['cidcard'];
            $data['password']=md5('tmd'.$param['password']);
            $data['status']=1;
            $data['uptime']=time();
            $res= Db::name('customer')->where(array('id'=>$param['id']))->update($data);
            return json(['code' => 200, 'data' =>$res, 'msg' => '更新成功']);
        }
    }
    public function state_off(){
        $id=input('param.id');
        $status = Db::name('customer')->where(array('id'=>$id))->update(['status'=>2]);
        if($status){
            return json(['code' => 200, 'data' =>$status, 'msg' => '已禁止']);
        }else{
            return json(['code' => 500, 'data' =>$status, 'msg' => '出错']);
        }
    }
    public function state_on(){
        $id=input('param.id');
        $status = Db::name('customer')->where(array('id'=>$id))->update(['status'=>1]);
        if($status){
            return json(['code' => 200, 'data' =>$status, 'msg' => '已开启']);
        }else{
            return json(['code' => 500, 'data' =>$status, 'msg' => '出错']);
        }
    }
    public function customer_del(){
        $param = input('post.');
        $res= Db::name('customer')->where(array('id'=>$param['id']))->update(['status'=>3]);
        Db::name('product')->where(array('cus_id'=>$param['id']))->update(['status'=>3]);
        return json(['code' => 200, 'data' =>$res, 'msg' => '删除成功']);

    }
    public function customer_dels(){
        $param = input('post.');
        $res= Db::name('customer')->where(array('id'=>$param['id']))->update(['status'=>3]);
        Db::name('product')->where(array('cus_id'=>$param['id']))->update(['status'=>3]);
        return json(['code' => 200, 'data' =>$res, 'msg' => '删除成功']);

    }
}
